<template>
  <component
    is="a-modal"
    v-model:visible="visible"
    :width="600"
    :title="title"
    :mask-closable="false"
    :ok-loading="loading"
    @cancel="close"
    @before-ok="submit">
    <!-- 表单信息 start -->
    <a-form ref="formRef" :model="formData" :rules="rules" :auto-label-width="true">
      <a-row :gutter="10">
        <a-col :span="24">
          <a-form-item label="用户名" field="username">
            <a-input v-model="formData.username" placeholder="请输入用户名" />
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="昵称" field="nickname">
            <a-input v-model="formData.nickname" placeholder="请输入昵称" />
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="密码" field="password">
            <a-input v-model="formData.password" placeholder="请输入密码" />
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="头像" field="avatar">
            <sa-upload-image v-model="formData.avatar" :limit="1" :multiple="false" />
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="金币余额" field="coin_balance">
            <a-input v-model="formData.coin_balance" placeholder="请输入金币余额" />
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="钻石余额" field="diamond_balance">
            <a-input v-model="formData.diamond_balance" placeholder="请输入钻石余额" />
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="启用状态" field="status">
            <sa-select v-model="formData.status" dict="yes_or_no" placeholder="请选择启用状态" allow-clear />
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="用户等级" field="level">
            <a-select
              v-model="formData.level"
              :field-names="{ label: 'name', value: 'id' }"
              :options="optionData.level"
              placeholder="请选择用户等级"
              allow-clear
             />
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="折扣" field="discount">
            <a-input v-model="formData.discount" placeholder="请输入折扣" />
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="会员到期时间" field="level_endtime">
            <a-date-picker v-model="formData.level_endtime" :show-time="true" mode="date" placeholder="请选择会员到期时间" />
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="注册 IP" field="reg_ip">
            <a-input v-model="formData.reg_ip" placeholder="请输入注册 IP" />
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="最后登录时间" field="last_login_time">
            <a-date-picker v-model="formData.last_login_time" :show-time="true" mode="date" placeholder="请选择最后登录时间" />
          </a-form-item>
        </a-col>
      </a-row>
    </a-form>
    <!-- 表单信息 end -->
  </component>
</template>

<script setup>
import { ref, reactive, computed } from 'vue'
import { Message, Modal } from '@arco-design/web-vue'
import commonApi from '@/api/common'
import api from '../api/user'

const emit = defineEmits(['success'])
// 引用定义
const visible = ref(false)
const loading = ref(false)
const formRef = ref()
const mode = ref('')
const optionData = reactive({
  level: [],
})

let title = computed(() => {
  return '用户管理' + (mode.value == 'add' ? '-新增' : '-编辑')
})

// 表单信息
const formData = reactive({
  id: null,
  username: '',
  nickname: '',
  password: '',
  openid: null,
  avatar: '',
  coin_balance: '0.00',
  diamond_balance: '0.00',
  status: 1,
  level: 1,
  discount: '0.00',
  level_endtime: '',
  reg_ip: '',
  last_login_time: '',
})

// 验证规则
const rules = {
  username: [{ required: true, message: '用户名必需填写' }],
  nickname: [{ required: true, message: '昵称必需填写' }],
  coin_balance: [{ required: true, message: '金币余额必需填写' }],
  diamond_balance: [{ required: true, message: '钻石余额必需填写' }],
  status: [{ required: true, message: '启用状态必需填写' }],
  level: [{ required: true, message: '用户等级必需填写' }],
  discount: [{ required: true, message: '折扣必需填写' }],
}

// 打开弹框
const open = async (type = 'add') => {
  mode.value = type
  visible.value = true
  formRef.value.resetFields()
  await initPage()
}

// 初始化页面数据
const initPage = async () => {
  const resp_level = await commonApi.commonGet('/ai/Level/index?saiType=all')
  optionData.level = resp_level.data
}

// 设置数据
const setFormData = async (data) => {
  for (const key in formData) {
    if (data[key] != null && data[key] != undefined) {
      formData[key] = data[key]
    }
  }
}

// 数据保存
const submit = async (done) => {
  const validate = await formRef.value?.validate()
  if (!validate) {
    loading.value = true
    let data = { ...formData }
    let result = {}
    if (mode.value === 'add') {
      // 添加数据
      data.id = undefined
      result = await api.save(data)
    } else {
      // 修改数据
      result = await api.update(data.id, data)
    }
    if (result.code === 200) {
      Message.success('操作成功')
      emit('success')
      done(true)
    }
    // 防止连续点击提交
    setTimeout(() => {
      loading.value = false
    }, 500)
  }
  done(false)
}

// 关闭弹窗
const close = () => (visible.value = false)

defineExpose({ open, setFormData })
</script>
